//
//  NetAPIManager.m
//  ImGuider
//
//  Created by llt on 2017/4/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "NetAPIManager.h"
#import "NetClient.h"
#import "FileManager.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "PhoneInfo.h"

#import "VersionModel.h"

#import "UMessage.h"

#import <QiniuSDK.h>
#import <HappyDNS.h>

#define UMessageGroup @"ImGuider"

@implementation NetAPIManager

//1.1 首页根据坐标获取城市及推荐景点
+ (void)getRecommendScenicWithLocation:(CLLocationCoordinate2D )coordinate andBlock:(void (^)(City *city, NSError *error))block {
    
    NSDictionary *params = @{@"lng": @(coordinate.longitude),@"lat":@(coordinate.latitude)};
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"homepage/geo" withParams:params needCache:NO withMethodType:Post autoShowError:NO andBlock:^(id data, NSError *error) {
        
        City *city = [City yy_modelWithJSON:data];
        
        if (block) {
            block(city,error);
        }
        
    }];
}
//1.2 根据城市ID获取城市信息及景点
+ (void)getRecommendScenicWithCityID:(NSNumber *)cityID andBlock:(void (^)(City *city, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"city/%@",cityID] withParams:nil needCache:YES withMethodType:Get autoShowError:YES andBlock:^(id data, NSError *error) {
        
        City *city = [City yy_modelWithJSON:data];
        
        if (city) {
            [AppManager sharedManager].lastCityID = city.cid;
        }
        
        
        if (block) {
            block(city,error);
        }
        
    }];
}

//1.20 获取当前坐标获所在城市名
+ (void)getGpsCityBlock:(void (^)(City *city, NSError *error))block {
    
    CLLocationCoordinate2D coor = transformFromGCJToWGS([LocationManager sharedManager].currentLocation.coordinate, nil);
    
    NSDictionary *params = @{@"lng": @(coor.longitude),@"lat":@(coor.latitude)};
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"gpscity" withParams:params needCache:NO withMethodType:Post autoShowError:NO andBlock:^(id data, NSError *error) {
        
        City *city = [City yy_modelWithJSON:data];
        
        if (block) {
            block(city,error);
        }
        
    }];
}


//0.1 获取版本号
+ (void)getAppVersionBlock:(void (^)(id data, NSError *error))block {
    
    [[NetClient sharedClient]requestJsonDataWithPath:@"version" withParams:nil needCache:NO withMethodType:Get autoShowError:NO andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                
                block (nil, error);
            }
            
            return;
        }
        
        VersionModel *versionModel = [VersionModel yy_modelWithJSON:data];
        
        [AppManager sharedManager].version = versionModel;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateApp" object:nil];
        
        
        if (![AppManager sharedManager].cityver || ![versionModel.cityver isEqualToString:[AppManager sharedManager].cityver]) {
            
            [self getCityListWithVersion:versionModel.cityver block:nil];
        }
        
        if (![AppManager sharedManager].countryver || ![versionModel.cityver isEqualToString:[AppManager sharedManager].countryver]) {
            
            [self getCountryListWithVersion:versionModel.cityver block:nil];
        }
        
        if (block) {
            block(@"success",error);
        }
        
    }];
}
//1.3 获取上架所有城市信息
+ (void)getCityListWithVersion:(NSString *)version block:(void (^)(NSString * result, NSError * error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"allcities" withParams:nil needCache:NO withMethodType:Get autoShowError:NO andBlock:^(id data, NSError *error) {
        
        if (error) {
            
            if (block) {
                block(nil,error);
            }
            return;
        }
        
        NSArray *cityArray = [NSArray yy_modelArrayWithClass:[City class] json:data[@"cities"]];
        
        NSArray * countryList = data[@"countries"];
        
        [AppManager sharedManager].countryList = countryList;
        BOOL success;
        
        NSString *result;
        
        [CityListDAO insertCityArray:cityArray success:&success];
        if (success) {
            [AppManager sharedManager].cityver = version;
            result = @"success";
        }
        
        if (block) {
            block(result,error);
        }
    }];
}
//0.2 获取所有国家列表
+ (void)getCountryListWithVersion:(NSString *)version block:(void (^)(NSString * result, NSError * error))block  {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"country/alllist" withParams:@{} needCache:NO withMethodType:Post autoShowError:NO andBlock:^(NSDictionary *data, NSError *error) {
        if (error) {
            return;
        }
        
        NSArray *countryArray = [NSArray yy_modelArrayWithClass:[Country class] json:data[@"country"]];
        
        BOOL success;
        
        NSString *result;
        
        [CountryListDAO insertCountryArray:countryArray success:&success];
        
        if (success) {
            [AppManager sharedManager].countryver = version;
            result = @"success";
        }
        
        if (block) {
            block(result,error);
        }
    }];
}
//1.4 获取城市下的景点
+ (void)getAllViewsWithCity:(NSNumber *)cityID start:(NSNumber *)start count:(NSNumber *)count block:(void (^)(NSArray *viewsArray, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"city/%@/views/v13",cityID] withParams:@{@"pageno":start,@"pagesize":count} needCache:YES  withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *resultArray = [NSArray yy_modelArrayWithClass:[Scenic class] json:data[@"content"]];
        
        if (block) {
            block (resultArray, error);
        }
    }];
}
//1.6 城市导览列表
+ (void)getAllLinesBy:(NSNumber *)cityID start:(NSNumber *)start count:(NSNumber *)count block:(void (^)(NSArray *, NSError *))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"city/%@/lines",cityID] withParams:@{@"pageno":start,@"pagesize":count} needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *lineArray = [NSArray yy_modelArrayWithClass:[LineModel class] json:data[@"content"]];
        if (block) {
            block (lineArray, error);
        }
    }];
}
//1.5 获取城市导游名片
+ (void)getAllGuiderBy:(NSNumber *)cityID start:(NSNumber *)start count:(NSNumber *)count block:(void (^)(NSArray *, NSError *))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"city/%@/guiders",cityID] withParams:@{@"pageno":start,@"pagesize":count} needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *guiderArray = [NSArray yy_modelArrayWithClass:[GuiderCardModel class] json:data[@"content"]];
        
        if (block) {
            block (guiderArray, error);
        }
    }];
}
//1.7 导游信息
+ (void)getGuiderDetailWith:(NSString *)guiderID start:(NSNumber *)start count:(NSNumber *)count block:(void (^)(GuiderCardModel *guider, NSError *error))block {
    
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionaryWithDictionary:@{@"pageno":start,@"pagesize":count}];
    
    NSString *cityid = [[AppManager sharedManager].lastCityID stringValue];
    
    if (cityid) {
        [parmas setValue:cityid forKey:@"cityid"];
    }
    
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"guider/%@",guiderID] withParams:parmas needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        GuiderCardModel *guiderModel = [GuiderCardModel yy_modelWithJSON:data];
        
        if (block) {
            block (guiderModel, error);
        }
        
    }];
    
}
//1.8 景点及导游信息
+ (void)getScenicGuiderListWithScenticID:(NSString *)scenicID Block:(void (^)(Scenic *scenic, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"viewandguides/%@/v2",scenicID] withParams:nil needCache:YES withMethodType:Get autoShowError:YES andBlock:^(id data, NSError *error) {
        
        Scenic *scenic = [Scenic yy_modelWithJSON:data];
        
        if (block) {
            block(scenic,error);
        }
    }];
}
//1.9 讲解线路详情
+ (void)getLineDetailWithLinID:(NSString *)lineID block:(void (^)(LineModel * model, NSError * error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"lineinfo/%@",lineID] withParams:nil needCache:YES withMethodType:Get autoShowError:YES andBlock:^(id data, NSError *error) {
        
        LineModel *model = [LineModel yy_modelWithJSON:data];
        
        if (block) {
            block(model, error);
        }
        
    }];
}
//2.1 发送验证码
+ (void)getCodeWithCountryCode:(NSString *)countrycode cellPhone:(NSString *)cellPhone isResetPWS:(BOOL)isReset Block:(void (^)(NSString * result, NSError * error))block {
    //   regist 注册时发送0，找回密码时发送1 ....烦死人
    NSDictionary *paramsDict = @{@"countrycode":countrycode,@"cellphone":cellPhone,@"regist":@(isReset ? 1 : 0)};
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"sendcode" withParams:paramsDict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        if (error) {
            if (block) {
                block(nil,error);
            }
            return;
        }
        if (block) {
            block(data,error);
        }
    }];
}
//2.2 用户手机注册
+ (void)phoneRegisterWithCountryCode:(NSString *)countrycode cellPhone:(NSString *)cellPhone encode:(NSString *) encode psw:(NSString *)psw block:(void (^)(NSString * result, NSError * error))block {
    
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionaryWithDictionary:@{@"countrycode":countrycode,@"username":cellPhone,@"encode":encode,@"password":psw}];
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"tourist" withParams:paramsDict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            
            return;
        }
        
        UserInfo *userInfo = [UserInfo yy_modelWithJSON:data];
        
        
        [UMessage addAlias:userInfo.uid type:UMessageGroup response:nil];
        
        [UserManager sharedManager].user = userInfo;
        
        [UserManager sharedManager].token = data[@"token"];
        [NetClient changeClient];
        
        [UserManager sharedManager].loginName = cellPhone;
        [UserManager sharedManager].loginCode = countrycode;
        [SAMKeychain setPassword:psw forService:kKeychainService account:cellPhone];
        
        if (block) {
            
            block(MYLocalizedString(@"LOGIN_SUCCESS", nil),nil);
        }
    }];
}
//2.3 用户第三方注册
+ (void)thirdRegisterType:(NSDictionary *)params block:(void (^)(NSString * result, NSError * error))block {
    
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionaryWithDictionary:params];
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"tourist3rd" withParams:paramsDict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            
            return;
        }
        
        UserInfo *userInfo = [UserInfo yy_modelWithJSON:data];
        
        
        [UMessage addAlias:userInfo.uid type:UMessageGroup response:nil];
        
        [UserManager sharedManager].user = userInfo;
        
        [UserManager sharedManager].token = data[@"token"];
        [NetClient changeClient];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshUserNotif object:nil];
        
        if (block) {
            block(MYLocalizedString(@"LOGIN_SUCCESS", nil),nil);
        }
        
    }];
}
//2.4 用户手机登录
+ (void)phoneLoginWithCountryCode:(NSString *)countrycode cellPhone:(NSString *)cellPhone psw:(NSString *)psw block:(void (^)(NSString * result, NSError * error))block {
    
    NSDictionary *paramsDict = @{@"countrycode":countrycode,@"username":cellPhone,@"password":psw};
    [[NetClient sharedClient] requestJsonDataWithPath:@"tourist/login" withParams:paramsDict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            return;
        }
        
        UserInfo *userInfo = [UserInfo yy_modelWithJSON:data];
        
        
        [UMessage addAlias:userInfo.uid type:UMessageGroup response:nil];
        
        [UserManager sharedManager].user = userInfo;
        
        [UserManager sharedManager].loginName = cellPhone;
        [UserManager sharedManager].loginCode = countrycode;
        [SAMKeychain setPassword:psw forService:kKeychainService account:cellPhone];
        
        
        [UserManager sharedManager].token = data[@"token"];
        [NetClient changeClient];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshUserNotif object:nil];
        
        if (block) {
            block(MYLocalizedString(@"LOGIN_SUCCESS", nil),nil);
        }
        
    }];
}
//2.5 获取用户信息
+ (void)getUserInfoWithBlock:(void (^)(NSString * result, NSError * error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"userinfo" withParams:nil needCache:NO withMethodType:Get autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            return;
        }
        
        UserInfo *userInfo = [UserInfo yy_modelWithJSON:data];
        
        
        [UMessage addAlias:userInfo.uid type:UMessageGroup response:nil];
        
        [UserManager sharedManager].user = userInfo;
        
        if (block) {
            block(@"ok",nil);
        }
    }];
}
//2.8 退出登录
+ (void)logoutWithBlock:(void (^)(NSString * result, NSError * error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"logout" withParams:nil needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            return;
        }
        
        [UserManager sharedManager].user = nil;
        [UserManager sharedManager].token = nil;
        
        [NetClient changeClient];
        [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshUserNotif object:nil];
        if (block) {
            block(data,nil);
        }
    }];
}
//2.9 更新用户密码
+ (void)resetPswWithCountryCode:(NSString *)countrycode cellPhone:(NSString *)cellPhone psw:(NSString *)psw encode:(NSString *)encode block:(void (^)(NSString * result, NSError * error))block {
    
    NSDictionary *paramsDict = @{@"countrycode":countrycode,@"username":cellPhone,@"password":psw,@"encode":encode};
    [[NetClient sharedClient] requestJsonDataWithPath:@"userpass" withParams:paramsDict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            return;
        }
        
        [UserManager sharedManager].loginName = cellPhone;
        [UserManager sharedManager].loginCode = countrycode;
        [SAMKeychain setPassword:psw forService:kKeychainService account:cellPhone];
        
        if (block) {
            block(data,nil);
        }
    }];
}
//2.6 更新用户头像
//2.7 更新用户信息
// type = 1 是头像
+ (void)updateUserinfo:(id)needData type:(NSInteger)type block:(void (^)(NSString * result, NSError * error))block {
    
    NSString *key = type == 1 ? @"head" : @"realname";
    NSString *url = type == 1 ? @"headimg" : @"userinfo";
    
    
    if (type == 1) {
        [[NetClient sharedClient] uploadImageWithImage:needData path:url name:key progress:nil andBlock:^(id data, NSError *error) {
            
            if (error) {
                if (block) {
                    block(nil,error);
                }
                return;
            }
            
            UserInfo *userInfo = [UserManager sharedManager].user;
            
            userInfo.headimg = data;
            
            [UserManager sharedManager].user = userInfo;
            
            if (block) {
                block(data,nil);
            }
            
        }];
        return;
    }
    
    
    NSDictionary *paramsDict = @{key : needData};
    
    [[NetClient sharedClient] requestJsonDataWithPath:url withParams:paramsDict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            return;
        }
        
        UserInfo *userInfo = [UserManager sharedManager].user;
        
        userInfo.username = data;
        
        [UserManager sharedManager].user = userInfo;
        
        if (block) {
            block(data,nil);
        }
    }];
}
//1.10 收藏
+ (void)collectWithType:(CollectType)collectType Id:(NSString *)favourid block:(void (^)(id result, NSError *error))block {
    
    NSString *type = @"recordset";
    
    if (collectType == CollectTypeGuider) {
        type = @"guider";
    }
    NSDictionary *paramDict = @{@"favourtype":type, @"favourid":favourid?favourid:@""};
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"favour" withParams:paramDict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        if (error) {
            if (block) {
                block(nil,error);
            }
            return;
        }
        if (block) {
            block(data[@"state"], error);
        }
        
    }];
}


//1.27 预下单接口

+ (void)preorderOrderWithLineID:(NSString *)lineid block:(void (^)(NSString *result, NSError *error))block {
    
    NSDictionary *params = nil;
    if (lineid) {
        params = @{@"lineid":lineid};
    }
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"pay/preorder" withParams:params needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (block) {
            block(data, error);
        }
    }];
}

//1.11 支付宝支付订单
//1.12 微信支付订单
//1.13 PayPal支付订单
//1.19 苹果内购支付APPLE
+ (void)getPaymentinfoWithPaymentID:(NSString *)paymentid couponid:(NSString *)couponid ordernumber:(NSString *)ordernumber payType:(PayType)payType merchType:(MerchType)merchType andBlock:(void (^)(id result, NSError *error))block {
    
    NSString *url = @"";
    switch (payType) {
            
        case PayTypeAlipay:
            url = @"pay/alipay";
            break;
        case PayTypeWX:
            url = @"pay/wechat";
            break;
        case PayTypePaypal:
            url = @"pay/paypal";
            break;
        case PayTypeApplepay:
            url = @"pay/";
            break;
        case PayTypeIAP:
            url = @"pay/iap";
            break;
            
        default:
            break;
    }
    
    NSString *type = @"";
    switch (merchType) {
            
        case MerchTypeLine:
            type = @"line";
            break;
        case MerchTypeTicket:
            type = @"ticket";
            break;
            
        default:
            break;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if (type) {
        [params setObject:type forKey:@"type"];
    }
    
    if (paymentid) {
        [params setObject:paymentid forKey:@"id"];
    }
    
    if (couponid) {
        [params setObject:couponid forKey:@"couponid"];
        
    }
    
    if (ordernumber) {
        [params setObject:ordernumber forKey:@"ordernumber"];
    }
    
    [[NetClient sharedClient] requestJsonDataWithPath:url withParams:params needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (block) {
            block(data,error);
        }
    }];
    
}
//1.14 验证支付宝订单是否成功
//1.16 验证微信订单是否成功
//1.18 PayPal验证
//1.20 苹果内购支付验证
+ (void)verifyPaymentOrderID:(NSString*)orderid notorderid:(NSString *)notorderid currency:(NSString *)currency payType:(PayType)payType andBlock:(void (^)(id result, NSError *error))block {
    
    NSString *url = @"";
    switch (payType) {
            
        case PayTypeAlipay:
            url = @"pay/alipay/check";
            break;
        case PayTypeWX:
            url = @"pay/wechat/check";
            break;
        case PayTypePaypal:
            url = @"pay/paypal/check";
            break;
        case PayTypeApplepay:
            url = @"pay//check";
            break;
        case PayTypeIAP:
            url = @"pay/iap/check";
            
            if (notorderid.length == 0) {
                if (block) {
                    block(nil,[NSError errorWithDomain:[AppManager sharedManager].baseURLStr code:-1 userInfo:@{@"msg":@""}]);
                }
                return;
            }
            
            break;
            
        default:
            break;
    }
    
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    
    if (orderid) {
        [paramsDict setObject:orderid forKey:@"orderid"];
    }
    
    if (notorderid) {
        [paramsDict setObject:notorderid forKey:@"notorderid"];
    }
    
    if (!currency) {
        currency = [AppManager sharedManager].currentCountryCode;
    }
    
    if (currency) {
        [paramsDict setObject:currency forKey:@"currency"];
    }
    
    [[NetClient sharedClient] requestJsonDataWithPath:url withParams:paramsDict needCache:NO withMethodType:Post autoShowError:YES retryingNumberOfTimes:@(5) andBlock:^(id data, NSError *error) {
        
        if (block) {
            block(data,error);
        }
    }];
    
}
//2.10 我的收藏
+ (void)getCollectBlock:(void (^)(NSArray <GuiderCardModel *>*guiderArray, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"myfavour" withParams:nil needCache:NO withMethodType:Get autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *guiderArray = [NSArray yy_modelArrayWithClass:[GuiderCardModel class] json:data[@"guidefavours"]];
        
        [guiderArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            ((GuiderCardModel *)obj).isfavor = YES;
        }];
        
        if (block) {
            block(guiderArray, error);
        }
        
    }];
}

//2.11 我的订单
+ (void)getOrderStart:(NSNumber *)start count:(NSNumber *)count Block:(void (^)(NSArray <OrderModel *>*orderArray, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"myorders" withParams:@{@"pageno":start,@"pagesize":count} needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *orderArray = [NSArray yy_modelArrayWithClass:[OrderModel class] json:data[@"orders"]];
        
        if (block) {
            block(orderArray, error);
        }
        
    }];
}
//2.12 问题反馈
+ (void)feedback:(NSNumber *)type content:(NSString *)content account:(NSString *)account Block:(void (^)(NSString *result, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"advice" withParams:@{@"type":type,@"content":content, @"account":account} needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (block) {
            block(data, error);
        }
        
    }];
}
//2.13 用户评价
+ (void)evaluate:(NSString *)lineid score:(NSNumber *)score comment:(NSString *)comment Block:(void (^)(NSString *result, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"grade" withParams:@{@"lineid":lineid,@"score":score, @"comment":comment} needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        if (block) {
            block(data, error);
        }
        
    }];
}
//2.14 我的优惠券
+ (void)getMycouponsWithLineid:(NSString *)lineid Block:(void (^)(NSArray <Coupon *>*couponArray, NSError *error))block {
    
    NSDictionary *dict = @{};
    
    if (lineid.length > 0) {
        dict = @{@"lineid" : lineid};
    }
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"mycoupons" withParams:dict needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *couponArray = [NSArray yy_modelArrayWithClass:[Coupon class] json:data];
        
        if (block) {
            block(couponArray, error);
        }
        
    }];
}
//0.3 用户数据收集
+ (void)postPhoneData:(NSDictionary *)params {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:params];
    [dict setObject:kVersion forKey:@"Version"];
    [dict setObject:[[PhoneInfo shareInfo] deviceUUID] forKey:@"deriveID"];
    [dict setObject:[UserManager sharedManager].user.uid ? [UserManager sharedManager].user.uid : @"" forKey:@"userID"];
    [dict setObject:@"iOS" forKey:@"channel"];
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"behavior/da" withParams:dict needCache:NO withMethodType:Post autoShowError:NO andBlock:nil];
}
//2.15 兑换优惠券
+ (void)redeemCouponCode:(NSString *)code Block:(void (^)(NSArray <Coupon *>*couponArray, NSString *errmsg))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"redeem/v2" withParams:@{@"code":code} needCache:NO withMethodType:Post autoShowError:NO andBlock:^(id data, NSError *error) {
        
        NSString *errmsg = [IGUtils tipFromError:error];
        
        if ([errmsg isEqualToString:@"noredeem"]) {
            
            errmsg = MYLocalizedString(@"VOUCHER_ERROR_CODE_INVALID", nil);
        } else if ([errmsg isEqualToString:@"used"]) {
            
            errmsg = MYLocalizedString(@"VOUCHER_ERROR_CODE_USED", nil);
        } else if ([errmsg isEqualToString:@"notrmb"]) {
            
            errmsg = MYLocalizedString(@"VOUCHER_ERROR_CODE_INVALID", nil);
        }
        
        NSArray *couponArray = [NSArray yy_modelArrayWithClass:[Coupon class] json:data];
        
        if (block) {
            block(couponArray, errmsg);
        }
    }];
}

//2.16 优惠券跳转
+ (void)useCouponid:(NSString *)couponid Block:(void (^)(NSString *lineid, NSString *errmsg))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"usecoupon/%@",couponid] withParams:@{} needCache:NO withMethodType:Post autoShowError:NO andBlock:^(id data, NSError *error) {
        
        NSString *errmsg = [IGUtils tipFromError:error];
        
        if ([errmsg isEqualToString:@"noredeem"]) {
            
            errmsg = MYLocalizedString(@"VOUCHER_ERROR_CODE_INVALID", nil);
        } else if ([errmsg isEqualToString:@"used"]) {
            
            errmsg = MYLocalizedString(@"VOUCHER_ERROR_CODE_USED", nil);
        } else if ([errmsg isEqualToString:@"notrmb"]) {
            
            errmsg = MYLocalizedString(@"VOUCHER_ERROR_CODE_INVALID", nil);
        }
        
        if (block) {
            block(data[@"lineid"], errmsg);
        }
    }];
}

//2.16.1 线路的提问列表
+ (void)getAllQuestionsWithLineID:(NSString *)lineid pageno:(NSNumber *)pageno pigesize:(NSNumber *)pagesize Block:(void (^)(NSDictionary * guiderInfo, NSArray <Question *>*questionArray, NSError *error))block {
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];//WithDictionary:@{@"pageno":pageno,@"pagesize":pagesize}];
    
    if (lineid) {
        [parmas setObject:lineid forKey:@"lineid"];
    }
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"qa/page/lineqa" withParams:parmas needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *questionArray = [NSArray yy_modelArrayWithClass:[Question class] json:data[@"content"]];
        
        if (block) {
            block(data,questionArray, error);
        }
        
    }];
}
//2.16.2 我的提问列表
+ (void)getMyQuestionBlock:(void (^)(NSArray <Question *>*questionArray, NSError *error))block {
    
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"qa/page/myqa" withParams:@{} needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *questionArray = [NSArray yy_modelArrayWithClass:[Question class] json:data[@"content"]];
        
        if (block) {
            block(questionArray, error);
        }
        
    }];
    
}
//1.23 讲解线路全部评论
+ (void)getAllCommentsWithLineID:(NSString *)lineid Block:(void (^)(NSArray<Comment *> *, LineModel *, NSError *))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"line/%@/comments",lineid] withParams:@{} needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        LineModel * linemodel = [LineModel yy_modelWithJSON:data];
        
        
        NSArray *questionArray = [NSArray yy_modelArrayWithClass:[Comment class] json:data[@"content"]];
        
        
        if (block) {
            block(questionArray,linemodel,error);
        }
        
    }];
}

// ------------
//0.4 获取七牛的token

+ (void)getQiniuTokenBlock:(void (^)(NSString *token, NSError * error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"qiniu/token" withParams:nil needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        if (block) {
            block(data,error);
        }
    }];
}

//上传多张图片
+ (void)uploadImages:(NSArray *)imageArray Block:(void (^)(NSArray *keys, NSError *error))block {
    
    [self getQiniuTokenBlock:^(NSString *token, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
            }
            return ;
        }
        
        dispatch_group_t net_group = dispatch_group_create();
        
//        QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
//            builder.zone = [QNFixedZone zone0];
//        }];
//
        QNUploadManager *uploadManager = [QNUploadManager sharedInstanceWithConfiguration:nil];
        
        NSMutableArray *array = [[NSMutableArray alloc] init];
        
        for (UIImage *image in imageArray) {
            
            dispatch_group_enter(net_group);
            
            
            NSData *data = UIImageJPEGRepresentation([IGUtils fixOrientation:image], 0.5);
            
            if (!data) {
                
                if (block) {
                    block(nil,[NSError errorWithDomain:[AppManager sharedManager].baseURLStr code:-1 userInfo:@{@"errormsg":@"图片转换错误"}]);
                }
                
                dispatch_group_leave(net_group);
                return;
            }
            
            [uploadManager putData:data key:nil token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                
                if (info.statusCode == 200 && resp) {
                    
                    
                    if (!key) {
                        key = resp[@"key"];
                    }
                    if (key) {
                        [array addObject:key];
                        
                        NSLog(@"----------- 七牛 key %@", key);
                    }
                    
                } else {
                    
                    if (block) {
                        block(nil,info.error);
                    }
                }
                dispatch_group_leave(net_group);
                
            } option:nil];
        }
        
        dispatch_group_notify(net_group, dispatch_get_main_queue(), ^{
            
            if (array.count == imageArray.count) {

                if (block) {
                    block(array,error);
                }
            }
        });
    }];
    
}

//2.17 提问QA
+ (void)qaWithLineid:(NSString *)lineid question:(NSString *)question images:(NSArray *)imageArray Block:(void (^)(id data, NSError *error))block {
    
    [self uploadImages:imageArray Block:^(NSArray *keys, NSError *error) {
        
        if (error) {
            if (block) {
                block(nil,error);
                
            }
            return ;
        }
        
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:@{@"lineid":lineid,@"question":question ,@"qtype":@(1)}];
        
        if (keys.count > 0) {
            
            NSMutableString *keysStr = [NSMutableString string];
            for (NSString *key in keys) {
                
                if (key) {
                    [keysStr appendString:key];
                }
                
                if (![key isEqualToString:keys.lastObject]) {
                    [keysStr appendString:@","];
                }
            }
            
            [params setObject:keysStr forKey:@"pictures"];
        }
        
        [[NetClient sharedClient] requestJsonDataWithPath:@"qa/add" withParams:params needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
            
            if (block) {
                block(data,error);
            }
        }];
    }];
}
//1.25 是否购买过该线路
+ (void)getLineBoughtStateWithLineid:(NSString *)lineid Block:(void (^)(LineModel *model, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[@"hasbought" stringByAppendingPathComponent:lineid] withParams:@{} needCache:NO withMethodType:Get autoShowError:YES andBlock:^(id data, NSError *error) {
        
        LineModel *model = [LineModel yy_modelWithJSON:data];
        
        if (block) {
            block(model,error);
        }
    }];
}
//3.1.1 游客-攻略列表
+ (void)getRaidersBy:(NSNumber *)cityID start:(NSNumber *)start count:(NSNumber *)count block:(void (^)(NSArray *, NSError *))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"strategy/page?pageno=%@&pagesize=%@",start,count] withParams:@{@"city":cityID,@"type":@(1),@"status":@(2)} needCache:NO withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *raidersArray = [NSArray yy_modelArrayWithClass:[Raiders class] json:data[@"content"]];
        
        if (block) {
            block (raidersArray, error);
        }
        
    }];
    
}

//1.24 所有签约行家
+ (void)getAllGuidersBlock:(void (^)(NSArray *, NSError *))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"vipguiders" withParams:@{} needCache:YES withMethodType:Post autoShowError:YES andBlock:^(id data, NSError *error) {
        
        NSArray *guiderArray = [NSArray yy_modelArrayWithClass:[GuiderCardModel class] json:data[@"content"]];
        
        if (block) {
            block (guiderArray, error);
        }
    }];
}
//2.19 提问回复是否已读
+ (void)setMessgaeReadWithQid:(NSString *)qid Block:(void (^)(NSString *result, NSError *error))block {
    
    [[NetClient sharedClient] requestJsonDataWithPath:[NSString stringWithFormat:@"qa/%@/isread",qid] withParams:@{} needCache:YES withMethodType:Post autoShowError:NO andBlock:^(id data, NSError *error) {
        
        if (block) {
            block (data, error);
        }
    }];
}
//3.2.1 游客-统计点击次数
+ (void)clickCountWithID:(NSString *)relationid type:(NSString *)type {
    
    [[NetClient sharedClient] requestJsonDataWithPath:@"clickcount/add" withParams:@{@"relationid":relationid, @"type":type} needCache:NO withMethodType:Post autoShowError:NO andBlock:nil];
}

@end

